package pony.controller;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/MusicPlayerServlet")
public class MusicPlayerServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public MusicPlayerServlet() {
        super();
    }
    
    @Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doService(request, response);
	}
    
    @Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doService(request, response);
	}
	
	private void doService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("audio/mpeg");
		InputStream fileReader = 
			getServletContext().getResourceAsStream("/WEB-INF/res/FallingGrace.mp3");
		
		OutputStream sOutput = response.getOutputStream();
		byte[] buffer = new byte[1024*2];
		int status = 0;
		while((status = fileReader.read()) != -1) {
			sOutput.write(buffer);
		}
		fileReader.close();
		sOutput.close();
		
	}
	
}
